package com.laiqilin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.laiqilin.entity.Students;
import com.laiqilin.mapper.StudentsMapper;
import com.laiqilin.service.IStudentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.PageResult;

import java.util.List;

@Service
public class StudentsServiceImpl extends ServiceImpl<StudentsMapper, Students> implements IStudentsService {
    @Autowired
    private StudentsMapper studentsMapper;
    @Override
    public PageResult<Students> selectByPage(Integer page, Integer pageSize) {

        // 分页
        Page<Students> page1=new Page<>(page, pageSize);
        Page<Students> studentsPage=this.page(page1,null);
        System.out.println(studentsPage);
        return new PageResult<>(studentsPage.getRecords(), studentsPage.getTotal());
    }

    @Override
    public PageResult<Students> selectByCondition(Integer page, Integer pageSize, Students students) {
        IPage<Students> pages=new Page<Students>(page, pageSize);
        QueryWrapper<Students> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(students.getStuName()),"stuName",students.getStuName());
        if(students.getStuAge()==null){
            students.setStuAge(0);
        }

        queryWrapper.eq(students.getStuAge()>0,"stuAge",students.getStuAge());
        IPage<Students> studentsIPage=this.page(pages,queryWrapper);
        System.out.println(studentsIPage);
        return new PageResult<Students>(studentsIPage.getRecords(),studentsIPage.getTotal());
    }

    @Override
    public Boolean deleteStudent(String stuId) {
        QueryWrapper<Students> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("stuId",stuId);
        return this.remove(queryWrapper);
    }

    @Override
    public Boolean updateStudent(Students students) {
        return this.updateById(students);
    }

    @Override
    public int deleteChecked(List<Integer> students) {
        return studentsMapper.deleteBatchIds(students);
    }
}
